<template>
  <div class="test">

    <div class="btns">
      <el-button size="small" type="primary" @click="toAdd">添加</el-button>
    </div>


  <el-table :data="list" style="width: 100%" size="small">
      <el-table-column type="index" label="序号"> </el-table-column>
      <el-table-column prop="name" label="名称" width="180"> </el-table-column>
      <el-table-column prop="introduce" label="个人介绍"> </el-table-column>
      <el-table-column prop="icon" label="头像"> </el-table-column>
      <el-table-column prop="type" label="用户身份"> </el-table-column>
      
      <el-table-column label="操作" width="150" align="center"> 
          <template v-slot="scope">
               <el-button size="small" type="danger" @click="del(scope.row)">删除</el-button>
               <el-button size="small" type="primary" @click="edit(scope.row)">修改</el-button>
          </template>
      </el-table-column>

  </el-table>
    



  <el-dialog class="customer_modal" title="添加用户" :visible.sync="dialogVisible" width="60%">
    <!-- <el-form :model="form" label-width="100px">
        <el-form-item label="名称" >
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="个人介绍" >
          <el-input v-model="form.gender"></el-input>
        </el-form-item>
        <el-form-item label="用户身份" >
          <el-input v-model="form.gender"></el-input>
        </el-form-item>
    </el-form> -->

    <el-form ref="form" :model="form" label-width="80px">
  <el-form-item label="    姓  名:">
    <el-input v-model="form.name"></el-input>
  </el-form-item>
  <el-form-item label="用户身份">
    <el-checkbox-group v-model="form.type">
      <el-checkbox label="会  员" name="type"></el-checkbox>
      <el-checkbox label="管理员" name="type"></el-checkbox>
      <el-checkbox label="普通用户" name="type"></el-checkbox>
    </el-checkbox-group>
  </el-form-item>
  <el-form-item label="个人介绍">
    <el-input type="textarea" v-model="form.introduce"></el-input>
  </el-form-item>
    </el-form>
      
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="save" size="small">确 定</el-button>
      </span>

  </el-dialog>

  </div>
</template>

<script>
// 导入request , 浏览器提供给我们的用于与后台进行交互的工具 api
import {get,post} from '@/utils/request'
export default {

  data (){
    return {
      type:{},
      introduce:{},
      form:{},
      list:[],
      dialogVisible:false
    }
  },

  created(){
    this.loadData();
  },

  methods:{

    // 保存
    save(){
      let url = "/privilege/SaveOrUpdate"
      post(url,this.form).then( (response)=>{
        // 1. 模态框关闭
        this.dialogVisible = false;
        // 2. 提示
        this.$Message({type:"success",Message:response.Message})
        // 3. 刷新
        this.loadData();
      } )
    },

    // 删除
    del(row){
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let url = "/privilege/delete"
          get(url,{id:row.id}).then( (response)=>{
            // 1. 提示
            this.$Message({type:"success",Message:response.Message})
            // 2. 刷新 
            this.loadData();
          } )
        })
    },

    onSubmit() {
        console.log('submit!');
      },

    // 编辑
    edit(row){
      this.form = row;
      this.dialogVisible = true;
    },

    // 加载数据
    loadData(){
      let url = "/privilege/findAll";
      get(url).then( (response)=>{
        // response就是后端返回给我们的Message{status,message,data,timestamp}
        this.list = response.data;
      } );
    },

    toAdd(){
      this.dialogVisible = true;
    }
    
  }
}
</script>
<style>

</style>